package dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

/**
 * Create By IntelliJ IDEA
 *
 * @Author: jsonor
 * @Create-Time: 2017/9/21 10:20
 * @Description: 用户信息的查询
 */
public class UserInfoDao {

  // 如果以后不使用DButils，和层的LoginServlet一点关系都没有
  private QueryRunner runner;

  private static final ComboPooledDataSource DS;

  static {
    // 连接池
    DS = new ComboPooledDataSource();
  }

  public UserInfoDao() {
    runner = new QueryRunner(DS);
  }

  /**
   * 通过名字查找用户的信息
   *
   * @param name 名字
   * @return 用户的信息
   */
  public UserInfo getByName(String name) throws SQLException {
    UserInfo user = runner.query("select * from userInfo where name = ?",
        new BeanHandler<UserInfo>(UserInfo.class), name);

    return user;
  }
}
